<!DOCTYPE html "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">


<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="gray-bg">
<div class="wrapper wrapper-content ">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-content">
                    <form class="form-horizontal m-t" id="signupForm">
                        <input type="hidden" id="id" name="id">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">年VIP充值金额：</label>
                            <div class="col-sm-4">
                                <input type="number" id="rechargeAmt" name="rechargeAmt"
                                       data-placeholder="年VIP充值金额" class="form-control" >
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">终生VIP充值金额：</label>
                            <div class="col-sm-4">
                                <input type="number" id="longTermRechargeAmt" name="longTermRechargeAmt"
                                       data-placeholder="终生VIP充值金额" class="form-control" >
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">微信二维码：</label>
                            <div class="col-sm-6">
                                <input @change="fileUpload($event)" accept="image/*" type="file">
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-sm-8 col-sm-offset-3">
                                <img id="wepayImg" alt="微信二维码" class="img-responsive img-thumbnail"
                                     style="height: 200px;width: 200px">
                                <input type="hidden" name="wepayImgUrl" id="wepayImgUrl">
                            </div>
                        </div>

                        <div class="form-group" id="cardDiv1" >
                            <label class="col-sm-2 control-label">支付宝二维码：</label>
                            <div class="col-sm-6">
                                <input @change="fileUploadCard($event)" accept="image/*" type="file">
                            </div>
                        </div>

                        <div class="form-group" id="cardDiv2">
                            <div class="col-sm-8 col-sm-offset-3">
                                <img id="alipayImg" alt="支付宝二维码" class="img-responsive img-thumbnail"
                                     style="height: 200px;width: 200px">
                                <input type="hidden" name="alipayImgUrl" id="alipayImgUrl">
                            </div>
                        </div>

                        <div class="text-right form-group">
                            <button type="submit" class="btn btn-primary">保存</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<div th:include="include::footer"></div>
<script th:inline="javascript">
    $().ready(function () {
        validateRule();
        getInfo();
    });

    // 上传图片
    const vue = new Vue({
        el: '#signupForm',
        data: {
            reqData: {}
        },
        methods: {
            fileUpload: function (evt) {
                const files = evt.target.files;
                if (files.length == 0) {
                    return;
                }

                const file = files[0];
                if (!/image\/\w+/.test(file.type)) {
                    parent.layer.alert("请选择图像类型的文件！");
                    return;
                }

                let vm = this;

                const fileType = '.' + file.name.toLowerCase().split('.').pop();

                const reader = new FileReader();
                reader.onload = (function (file) {
                    return function (e) {
                        const result = e.target.result;
                        vm.imgDataURL = result;
                        const base64Str = result.split(",")[1];
                        //上传地址动态获取
                        var uploadUrl = "/api/fileUpload";
                        $.post(uploadUrl, {fileType, base64Str}, function (resp) {
                            if (!!resp) {
                                const base64 = new Base64();
                                const respData = JSON.parse(base64.decode(resp));
                                if (respData.code === 0) {
                                    console.log("wepayImgUrl",respData.data.imgPath);
                                    $("#wepayImgUrl").val(respData.data.imgPath);
                                    $('#wepayImg').attr('src',respData.data.imgPath);
                                }
                            }
                        });
                    }
                })(file);
                reader.readAsDataURL(file);
            },
            fileUploadCard: function (evt) {
                const files = evt.target.files;
                if (files.length == 0) {
                    return;
                }

                const file = files[0];
                if (!/image\/\w+/.test(file.type)) {
                    parent.layer.alert("请选择图像类型的文件！");
                    return;
                }

                let vm = this;

                const fileType = '.' + file.name.toLowerCase().split('.').pop();

                const reader = new FileReader();
                reader.onload = (function (file) {
                    return function (e) {
                        const result = e.target.result;
                        vm.cardimgDataURL = result;
                        const base64Str = result.split(",")[1];
                        //上传地址动态获取
                        var uploadUrl = "/api/fileUpload";
                        $.post(uploadUrl, {fileType, base64Str}, function (resp) {
                            if (!!resp) {
                                const base64 = new Base64();
                                const respData = JSON.parse(base64.decode(resp));
                                if (respData.code === 0) {
                                    $("#alipayImgUrl").val(respData.data.imgPath);
                                    $('#alipayImg').attr('src',respData.data.imgPath);
                                }
                            }
                        });
                    }
                })(file);
                reader.readAsDataURL(file);
            }
        }

    });

    $.validator.setDefaults({
        submitHandler: function () {
            save();
        }
    });

    function changeProdType(obj){
        if(obj.value == "1"){
            $('#lb_rate').hide();
            $('#div_rate').hide();
            $('#cardDiv1').show();
            $('#cardDiv2').show();
        }else if(obj.value == "2"){
            $('#lb_rate').show();
            $('#div_rate').show();
            $('#cardDiv1').hide();
            $('#cardDiv2').hide();
        }
    }
    function getInfo() {
        $.ajax({
            type: "GET",
            url: "/mp/product/getRecharge/1",
            dataType: "JSON",
            async: false,
            success: function (data) {
                console.log("data",data);
                if(data != null){
                    $('#id').val(data.id);
                    $('#rechargeAmt').val(data.rechargeAmt);
                    $('#longTermRechargeAmt').val(data.longTermRechargeAmt);
                    $('#wepayImgUrl').val(data.wepayImgUrl);
                    $('#alipayImgUrl').val(data.alipayImgUrl);
                    $('#wepayImg').attr('src',data.wepayImgUrl);
                    $('#alipayImg').attr('src',data.alipayImgUrl);
                }
            }
        });

    }

    function save() {
        //var content_sn = $("#content_sn").summernote('code');
        //$("#productDetail").val(content_sn);
        var wepayImgUrl = $("#wepayImgUrl").val();
        var alipayImgUrl= $("#alipayImgUrl").val();

        console.info('wepayImgUrl',wepayImgUrl);
        console.info('alipayImgUrl',alipayImgUrl);

        if(wepayImgUrl == null || wepayImgUrl == ''){
            layer.msg("请上传微信二维码");
            return false;
        }

        if(alipayImgUrl == null || alipayImgUrl == ''){
            layer.msg("请上传支付宝二维码");
            return false;
        }

        $.ajax({
            cache: true,
            type: "POST",
            url: "/mp/product/updateRecharge",
            data: $('#signupForm').serialize(), // 你的formid
            async: false,
            success: function (data) {
                layer.msg("操作成功");
            }
        });
    }

    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#signupForm").validate({
            rules: {
                rechargeAmt: {
                    required: true
                },
                longTermRechargeAmt: {
                    required: true
                }
            },
            messages: {
                rechargeAmt: {
                    required: icon + "必填"
                },
                longTermRechargeAmt: {
                    required: icon + "必填"
                }
            }
        })
    }

</script>
</body>
</html>